package com.aiss.ws.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiss.ws.Entity.TestItem;
import com.aiss.ws.R;

import java.util.List;

/**
 */
public class ChangeTitleAdatper extends BaseAdapter {

    private List<TestItem> names;
    private Context context;
    private Handler handler;

    public ChangeTitleAdatper(List<TestItem> names, Context context, Handler handler) {
        this.names = names;
        this.context = context;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return names.size();
    }

    @Override
    public Object getItem(int position) {
        return names.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView==null){
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.changetitle_item,null);
            holder.textView = (TextView) convertView.findViewById(R.id.textView);
            holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
            holder.rel = (RelativeLayout) convertView.findViewById(R.id.rel);
            convertView.setTag(holder);
        }else{
            holder = (Holder) convertView.getTag();
        }
        holder.textView.setText(position+"");
        if(names.get(position).getType().equals("单选题")||names.get(position).getType().equals("判断题")){
            if(names.get(position).getMe_answer()!=null){
                holder.textView.setVisibility(View.VISIBLE);
                holder.textView2.setVisibility(View.GONE);
            }else{
                holder.textView.setVisibility(View.GONE);
                holder.textView2.setVisibility(View.VISIBLE);
            }
        }else{//多选题
            if(names.get(position).getMe_more_answer()!=null){
                holder.textView.setVisibility(View.VISIBLE);
                holder.textView2.setVisibility(View.GONE);
            }else{
                holder.textView.setVisibility(View.GONE);
                holder.textView2.setVisibility(View.VISIBLE);
            }

        }
        holder.textView.setText(position+1+"");
        holder.textView2.setText(position+1+"");

        holder.rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = 1;
                msg.arg1 = position;
                handler.sendMessage(msg);
            }
        });



        return convertView;
    }

    class Holder{
        TextView textView;
        TextView textView2;
        RelativeLayout rel;
    }

}
